package com.ruoyi.business.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.ruoyi.business.controller.MobileExhibitorsController;
import com.ruoyi.business.domain.MobileExhibitors;
import com.ruoyi.business.mapper.MobileExhibitorsMapper;
import com.ruoyi.business.service.IMobileExhibitorsService;
import com.ruoyi.common.exception.MessageException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpReqRespUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;

/**
 * 参展商Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-13
 */
@Service
public class MobileExhibitorsServiceImpl extends ServiceImpl<MobileExhibitorsMapper, MobileExhibitors> implements IMobileExhibitorsService {

    private static final Logger logger = LoggerFactory.getLogger(MobileExhibitorsServiceImpl.class);

    private static final String TENCENT_LOCATION_SERVICE_URL = "https://apis.map.qq.com/ws/geocoder/v1/";
    private static final String KEY = "XC7BZ-SJ23I-T6BG4-U434Z-R5Z7H-UXFLP";



    @Autowired
    private MobileExhibitorsMapper mobileExhibitorsMapper;

    /**
     * 查询参展商
     *
     * @param id 参展商主键
     * @return 参展商
     */
    @Override
    public MobileExhibitors selectMobileExhibitorsById(Long id,String openId) {
        return mobileExhibitorsMapper.selectMobileExhibitorsById(id,openId);
    }

    /**
     * 查询参展商列表
     *
     * @param mobileExhibitors 参展商
     * @return 参展商
     */
    @Override
    public List<MobileExhibitors> selectMobileExhibitorsList(MobileExhibitors mobileExhibitors) {
        return mobileExhibitorsMapper.selectMobileExhibitorsList(mobileExhibitors);
    }

    /**
     * 新增参展商
     *
     * @param mobileExhibitors 参展商
     * @return 结果
     */
    @Override
    public int insertMobileExhibitors(MobileExhibitors mobileExhibitors) {
        mobileExhibitors.setCreateTime(DateUtils.getNowDate());
        return mobileExhibitorsMapper.insertMobileExhibitors(mobileExhibitors);
    }

    /**
     * 修改参展商
     *
     * @param mobileExhibitors 参展商
     * @return 结果
     */
    @Override
    public int updateMobileExhibitors(MobileExhibitors mobileExhibitors) {
        mobileExhibitors.setUpdateTime(DateUtils.getNowDate());
        return mobileExhibitorsMapper.updateMobileExhibitors(mobileExhibitors);
    }

    /**
     * 批量删除参展商
     *
     * @param ids 需要删除的参展商主键
     * @return 结果
     */
    @Override
    public int deleteMobileExhibitorsByIds(Long[] ids) {
        return mobileExhibitorsMapper.deleteMobileExhibitorsByIds(ids);
    }

    /**
     * 删除参展商信息
     *
     * @param id 参展商主键
     * @return 结果
     */
    @Override
    public int deleteMobileExhibitorsById(Long id) {
        return mobileExhibitorsMapper.deleteMobileExhibitorsById(id);
    }

    /**
     * @param mobileExhibitors
     * @return com.ruoyi.business.domain.MobileExhibitors
     * @Author liumaotong
     * @Description 根据地址查询GPS坐标
     * @Date 2024/6/16
     * @Param [mobileExhibitors]
     */
    @Override
    public JSONObject getGpsByAddress(MobileExhibitors mobileExhibitors) {
        //此处使用腾讯位置服务中的API获取地址的经纬度坐标
        String address = mobileExhibitors.getAddress();
        StringBuilder requestUrl = new StringBuilder();
        requestUrl.append(TENCENT_LOCATION_SERVICE_URL);
        requestUrl.append("?address=");
        requestUrl.append(address);
        requestUrl.append("&key=");
        requestUrl.append(KEY);
        JSONObject resultJson = HttpReqRespUtil.sendGet(requestUrl.toString());
        if(resultJson != null && resultJson.getInteger("status") == 0){
            logger.info("腾讯接口返回：" + resultJson.toJSONString());
            return resultJson.getJSONObject("result");
        }else {
            throw new MessageException(resultJson.toJSONString());
        }
    }
}